package com.haidechizi.fastadmin.modules.gen.controller;

import com.haidechizi.fastadmin.common.page.PageDto;
import com.haidechizi.fastadmin.common.utils.PageInfo;
import com.haidechizi.fastadmin.common.utils.Response;
import com.haidechizi.fastadmin.common.validator.ValidatorUtils;
import com.haidechizi.fastadmin.modules.gen.entity.GenTable;
import com.haidechizi.fastadmin.modules.gen.service.GenTableService;
import com.haidechizi.fastadmin.modules.gen.util.GenUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 业务表Controller
 */
@RestController
@RequestMapping(value = "gen/genTable/")
public class GenTableController {

    @Autowired
    private GenTableService genTableService;

    /**
     * 业务表列表
     *
     * @param pageDto
     * @param genTable
     * @return
     */
    @RequiresPermissions("gen:genTable:list")
    @RequestMapping(value = "list")
    public Response list(PageDto pageDto, GenTable genTable) {
        PageInfo page = genTableService.findPage(pageDto, genTable);
        return Response.ok().put("page", page);
    }

    /**
     * 查询当前库的所有表
     *
     * @return
     */
    @RequiresPermissions("gen:genTable:list")
    @RequestMapping(value = "tableList")
    public Response tableList() {
        List<GenTable> tableList = genTableService.findTableListFormDb(new GenTable());
        return Response.ok().put("tableList", tableList);
    }

    /**
     * 表的列字段
     *
     * @param genTable
     * @return
     */
    @RequiresPermissions("gen:genTable:list")
    @RequestMapping(value = "columnList")
    public Response columnList(GenTable genTable) {
        boolean check = genTableService.checkTableName(genTable.getName());
        if (!check) {
            return Response.error("表已经添加！");
        }
        genTable = genTableService.getTableFormDb(genTable);
        return Response.ok().put("genTable", genTable).put("config", GenUtils.getConfig());
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("gen:genTable:info")
    public Response info(@PathVariable("id") Long id) {
        GenTable genTable = genTableService.get(id);
        return Response.ok().put("genTable", genTable).put("config", GenUtils.getConfig());
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("gen:genTable:save")
    public Response save(@RequestBody GenTable genTable) {
        //校验类型
        ValidatorUtils.validateEntity(genTable);
        genTableService.save(genTable);
        return Response.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("gen:genTable:update")
    public Response update(@RequestBody GenTable genTable) {
        //校验类型
        ValidatorUtils.validateEntity(genTable);
        genTableService.update(genTable);
        return Response.ok();
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @RequiresPermissions("gen:genTable:delete")
    @RequestMapping(value = "delete")
    public Response delete(@RequestBody Long[] ids) {
        genTableService.delete(ids);
        return Response.ok();
    }

}
